package com.hczx.caxa.service;

import com.hczx.caxa.controller.CaxaApiController;
import io.minio.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

@Service
public class MinioService {

    private static Logger logger = LoggerFactory.getLogger(CaxaApiController.class);

    @Value("${minio.bucketName}")
    private String bucketName;
    @Value("${minio.endpoint}")
    private String endpoint;

    private final MinioClient minioClient;

    public MinioService(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    /**
     * description: 上传文件
     */
    public void upload(MultipartFile[] multipartFile, String contentType) {
        try {
            for (MultipartFile file : multipartFile) {
                String fileName = file.getOriginalFilename();
                InputStream inputStream = file.getInputStream();
                upload(inputStream, fileName, contentType);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * description: 上传文件
     */
    public String upload(MultipartFile multipartFile, String fileName, String contentType) {
        String filepath = StringUtils.EMPTY;
        try {
            logger.info("开始上传minio：{}", fileName);
            InputStream inputStream = multipartFile.getInputStream();
            filepath = upload(inputStream, fileName, contentType);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return filepath;
    }

    /**
     * description: 上传文件
     */
    public String upload(byte[] fileByte, String fileName, String contentType) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(fileByte);
        return upload(inputStream, fileName, contentType);
    }

    /**
     * description: 上传文件
     */
    public String upload(InputStream inputStream, String fileName, String contentType) {
        String filepath = StringUtils.EMPTY;
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, inputStream.available(), -1)
                    .contentType(contentType)
                    .build()
            );
            filepath = endpoint + "/" + bucketName + fileName;
            logger.info("上传minio结束：{}", filepath);
            IOUtils.closeQuietly(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filepath;
    }

    public byte[] download(String fileName) {
        byte[] bytes = new byte[]{};
        try {
            //文件下载
            GetObjectResponse response = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(fileName)
                            .build());

            bytes = IOUtils.toByteArray(response);
            IOUtils.closeQuietly(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * 判断文件是否存在
     *
     * @param objectName 文件名称, 如果要带文件夹请用 / 分割, 例如 /help/index.html
     * @return true存在, 反之
     */
    public Boolean checkFileIsExist(String objectName) {
        try {
            minioClient.statObject(StatObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .build());
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public void delete(String objectName) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .object(objectName)
                    .bucket(bucketName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
